package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.DailyTrainExample;
import com.jiawa.train.business.domain.DailyTrainStation;
import com.jiawa.train.business.domain.DailyTrainStationExample;
import com.jiawa.train.business.domain.Req.DailyTrainStationQueryReq;
import com.jiawa.train.business.domain.Req.DailyTrainStationSaveReq;
import com.jiawa.train.business.domain.TrainStation;
import com.jiawa.train.business.domain.resp.DailyTrainStationQueryResp;
import com.jiawa.train.business.mapper.DailyTrainStationMapper;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainStationService {

    @Autowired
    private DailyTrainStationMapper dailyTrainStationMapper;

    @Autowired
    private TrainStationService trainStationService;
    public PageResp<DailyTrainStationQueryResp> queryList(@Valid DailyTrainStationQueryReq req) {
        DailyTrainStationExample example = new DailyTrainStationExample();
        PageHelper.startPage(req.getPage(), req.getSize());
        List<DailyTrainStation> list = dailyTrainStationMapper.selectByExample(example);
        List<DailyTrainStationQueryResp> queryResps = BeanUtil.copyToList(list, DailyTrainStationQueryResp.class);

        PageInfo pageInfo = new PageInfo(list);
        PageResp<DailyTrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(queryResps);
        return pageResp;
    }

    public void save(DailyTrainStationSaveReq req) {
        DailyTrainStation dailyTrainStation = new DailyTrainStation();
        BeanUtil.copyProperties(req, dailyTrainStation);
        if(ObjectUtil.isNull(req.getId())){
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            dailyTrainStation.setCreateTime(new Date());
            dailyTrainStation.setUpdateTime(new Date());
            dailyTrainStationMapper.insert(dailyTrainStation);
        }else {
            dailyTrainStation.setUpdateTime(new Date());
            dailyTrainStationMapper.updateByPrimaryKeySelective(dailyTrainStation);
        }
    }

    public void delete(Long id) {
        dailyTrainStationMapper.deleteByPrimaryKey(id);
    }


    @Transactional
    public void genDaily(Date date,String trainCode){
        //删除某日某次的车站信息
        DailyTrainStationExample stationExample = new DailyTrainStationExample();
        stationExample.createCriteria()
                .andDateEqualTo(date)
                .andTrainCodeEqualTo(trainCode);
        dailyTrainStationMapper.deleteByExample(stationExample);

        //查出某车次的所有车站信息
        List<TrainStation> stationList = trainStationService.selectByTrainCode(trainCode);
        if(CollUtil.isEmpty(stationList)){
            return;
        }

        for(TrainStation station : stationList){
            DailyTrainStation trainStation = BeanUtil.copyProperties(station, DailyTrainStation.class);
            trainStation.setUpdateTime(new Date());
            trainStation.setCreateTime(new Date());
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setDate(date);
            dailyTrainStationMapper.insert(trainStation);
        }
    }
}
